package com.mv.spring.web.servlet;

import com.mv.spring.web.handler.HandlerManager;
import com.mv.spring.web.handler.MappingHandler;
import com.mv.webserver.http.HttpRequest;
import com.mv.webserver.http.HttpResponse;
import com.mv.webserver.http.HttpServlet;

import javax.servlet.ServletConfig;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;


public class DispatcherServlet extends HttpServlet  {
    private static final Logger log = LoggerFactory.getLogger(DispatcherServlet.class);
    
    @Override
    public void init(ServletConfig config) {

    }

    @Override
    public ServletConfig getServletConfig() {
        return null;
    }

    @Override
    public void service(ServletRequest req, ServletResponse res) throws IOException {
    	String servletUri = ((HttpServletRequest) req).getRequestURI();
    	log.info("service req url:" + servletUri);
        for (MappingHandler mappingHandler : HandlerManager.mappingHandlerList) {
            // 从所有的 MappingHandler 中逐一尝试处理请求，
            // 如果某个 handler 可以处理(返回true)，则返回即可
            try {
                log.info("outside tomcat request handle  url:" + mappingHandler.getUrl());
                if (!mappingHandler.getUrl().equals(servletUri)) {
                   continue;
                }
                mappingHandler.handle(req, res);
                return;
            } catch (IllegalAccessException | InvocationTargetException e) {
                e.printStackTrace();
                res.getWriter().println("505 Internet Error");
                return;
            }
        }
        res.getWriter().println("404,resource not found!");
    }

    @Override
    public String getServletInfo() {
        return null;
    }

    @Override
    public void destroy() {

    }

    //处理内部自实现tomcat请求
	@Override
	public void service(HttpRequest request, HttpResponse response) {
		String servletUri = request.getRequestURI();
		log.info("tomcat mv service req url:" + servletUri);
        for (MappingHandler mappingHandler : HandlerManager.mappingHandlerList) {
            // 从所有的 MappingHandler 中逐一尝试处理请求，
            // 如果某个 handler 可以处理(返回true)，则返回即可
            try {
                if (mappingHandler.handleInnerTomcat(request, response)) {
                    return;
                }
            } catch (IllegalAccessException | InvocationTargetException  | IOException e) {
                e.printStackTrace();
            }
        }
        //response.getWriter().println("failed!");
		
	}

	@Override
	public void doGet(HttpRequest request, HttpResponse response) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void doPost(HttpRequest request, HttpResponse response) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void doUpdate(HttpRequest request, HttpResponse response) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void doDelete(HttpRequest request, HttpResponse response) {
		// TODO Auto-generated method stub
		
	}
}
